package com.pandy.code;

/**
 * @description: 汉诺塔问题
 * @author: panhong
 * @date: 2025/5/6
 */
public class Code18_HanoiTower {

    public static void hanoi(int n) {
        leftToRight(n);
    }

    /**
     * 1 ~ n-1 从左 -> 中
     * n 从左 -> 右
     * 1 ~ n-1 从中 -> 右
     *
     * 以下几个方法，也是按照这种模式移动
     * @param n
     */
    private static void leftToRight(int n) {
        if(n == 1) {
            System.out.println("move 1 from left to right");
            return;
        }

        leftToMiddle(n-1);
        System.out.println("move " + n + " from left to right");
        middleToRight(n-1);
    }

    private static void leftToMiddle(int n) {
        if(n == 1) {
            System.out.println("move 1 from left to middle");
            return;
        }

        leftToRight(n-1);
        System.out.println("move " + n + " from left to middle");
        rightToMiddle(n-1);
    }

    private static void middleToRight(int n) {
        if(n == 1) {
            System.out.println("move 1 from middle to right");
            return;
        }
        middleToLeft(n-1);
        System.out.println("move " + n + " from middle to right");
        leftToRight(n-1);
    }

    private static void middleToLeft(int n) {
        if(n == 1) {
            System.out.println("move 1 from middle to left");
            return;
        }
        middleToRight(n-1);
        System.out.println("move " + n + " from middle to left");
        rightToLeft(n-1);
    }

    private static void rightToMiddle(int n) {
        if(n == 1) {
            System.out.println("move 1 from right to middle");
            return;
        }
        rightToLeft(n-1);
        System.out.println("move " + n + " from right to middle");
        leftToMiddle(n-1);
    }

    private static void rightToLeft(int n) {
        if(n == 1) {
            System.out.println("move 1 from right to left");
            return;
        }
        rightToMiddle(n-1);
        System.out.println("move " + n + " from right to left");
        middleToLeft(n-1);
    }

    /**
     * 将上面的过程，统一一个方法
     * @param n 几层
     * @param from 被移动方
     * @param to 目的地
     * @param other 辅助塔
     */
    public static void move(int n, String from,String to, String other) {
        if(n == 1) {
            System.out.println("move 1 from " + from + " to " + to);
            return;
        }
        move(n-1, from, other, to);
        System.out.println("move " + n + " from " + from + " to " + to);
        move(n-1, other, to, from);
    }

    public static void hanoi1(int n) {
        move(n, "left", "right", "middle");
    }





    public static void main(String[] args) {
        hanoi(3);
        System.out.println("----------------------");
        hanoi(3);
    }
}
